package Login

import (
	"MedicalTreatment/global"
	"MedicalTreatment/model"
	"fmt"
	"github.com/gin-gonic/gin"
)

func Register(r *gin.Engine) {
	r.POST("/reg", func(context *gin.Context) {
		var user model.RegisterText
		if err := context.ShouldBindJSON(&user); err != nil {
			fmt.Println(err)
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		fmt.Println(user)
		count := global.DB.Model(&model.RegisterText{}).Where("username = ?", user.Username).First(&user).RowsAffected
		if count == 0 {
			if err := global.DB.Create(&user).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})

			} else {
				context.JSON(200, gin.H{
					"Msg": "创建成功",
				})
			}
		} else {
			context.JSON(200, gin.H{
				"Msg": "用户重复",
			})
		}
	})
}
